package com.wude.guild.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.mc.developmentkit.callback.AliPayListener;
import com.mc.developmentkit.utils.ToastUtil;
import com.wude.bean.GetPaidInfo;
import com.wude.guild.R;
import com.wude.http.HttpCom;
import com.wude.tools.ThirdPartyPay;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Administrator on 2017/11/17 0017.
 * <p>
 * 选择充值方式
 */

public class PayDialog extends Dialog implements View.OnClickListener, AliPayListener {
    private Context context;
    private CheckBox zfb;
    private CheckBox wx;
    private TextView tv_pay;
    private String phone;
    private Activity activity;
    private ImageView iv_dissmiss;

    public PayDialog(@NonNull Context context, String phone, Activity activity) {
        super(context);
        this.context = context;
        this.phone = phone;
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去标题栏
        setContentView(R.layout.pay_dialog);
        // 宽度充满整个手机屏
        Window window = this.getWindow();
        window.setBackgroundDrawableResource(R.color.transparency);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //在手机屏幕下方
        window.setAttributes(lp);
        window.setGravity(Gravity.BOTTOM);

        initView();
    }

    private void initView() {
        zfb = findViewById(R.id.zfb);
        wx = findViewById(R.id.wx);
        tv_pay = findViewById(R.id.tv_pay);
        iv_dissmiss = findViewById(R.id.iv_dismiss);

        tv_pay.setOnClickListener(this);
        iv_dissmiss.setOnClickListener(this);

        zfb.setChecked(true);
        zfb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    wx.setChecked(false);
                }
            }
        });
        wx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    zfb.setChecked(false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_dismiss:
                dismiss();
                break;
            case R.id.tv_pay:
                pay();
                break;
        }
    }

    private void pay() {
        if (!zfb.isChecked() && !wx.isChecked()) {
            ToastUtil.showToast("请选择一种支付方式");
            return;
        }
        GetPaidInfo getPaidInfo = new GetPaidInfo();
        getPaidInfo.pay_amount = "100";                 //应付金额
        getPaidInfo.good_type = 3;                      //1平台币   2绑币  3话费充值
        getPaidInfo.user_sign = Utils.getLoginUser().token;        //用户token
        getPaidInfo.promote_id = "0";                   //推广员id
        getPaidInfo.phone = phone;
        if (zfb.isChecked()) {
            getPaidInfo.payType = "1";                  //支付宝
            ZFB(getPaidInfo);
        } else if (wx.isChecked()) {
            getPaidInfo.payType = "2";                  //微信
            WX(getPaidInfo);
        }
    }

    /**
     * 微信支付
     */
    private void WX(GetPaidInfo info) {
        ThirdPartyPay.getInstance().wxPay(activity, HttpCom.getPayUrl(), info);
    }

    /**
     * 支付宝支付
     */
    private void ZFB(GetPaidInfo info) {
        ThirdPartyPay.getInstance().aliPay(activity, HttpCom.getPayUrl(), info, this);
    }

    public void paySuccess() {
        PaySuccessDialog dialog = new PaySuccessDialog(context, activity, phone);
        dialog.show();
        dismiss();
    }

    @Override
    public void zfbPayResult(String s) {
        Log.e("支付宝返回", s);
        try {
            JSONObject jsonObject = new JSONObject(s);
            String resultStatus = jsonObject.getString("code");
            switch (resultStatus) {
                case "9000"://支付成功
                case "4001":
                    paySuccess();
                    break;
                case "8000": //等待支付结果
                    break;
                default://支付失败
                    ToastUtil.showToast("支付失败");
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
